<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Setting extends CI_Controller {

 function __construct()
 {
	parent::__construct();

    $this->load->model('m_impact', '', true);	
 }
 function index($offset = ''){
	/* pagination */	
	$selects	= "		tbl_setting.*,
						tbl_setting_type.name AS type_name
					  ";
	$joins		= array(
						  'tbl_setting_type'		=> array('tbl_setting.setting_type_id = tbl_setting_type.id', 'LEFT')
						   );
	$wheres = FALSE;	
	$likes = FALSE;
	
	$limit = FALSE;
	
	$data['settings'] = $this->m_impact->join_by_filter('tbl_setting', $selects, $joins, $wheres, $likes, '', 'id', 'DESC', '', $limit, $offset);
		 	
	$data['body'] ='setting/v_setting_list';
	$this->load->view('desktop/desktop',$data);
 }
 
 /*
 show page new
 */
 function page_new(){
	$where_type = array();
	$data['setting_types']	=	$this->m_impact->get_by_fields('tbl_setting_type',$where_type,'id');
	$data['body']='setting/v_page_new';
	$this->load->view('desktop/desktop',$data);
 }
 /*
 Insert data to DB
 */
 function save_new ($type = FALSE){ 
	
	$data_arr = array(
						'setting_type_id'			=> $this->m_impact->encrypt_decrypt('decrypt', $this->input->post('setting_type_id')),
						'name'			=> $this->input->post('name'),
						'lat'			=> $this->input->post('lat'),
						'lng'			=> $this->input->post('lng'),
						'description'	=> $this->input->post('description'), 
						'image' 		=> $this->input->post('image'), 
						'user_create' 	=> $this->session->userdata('name'), 
	 				    'create_date' 	=> date('Y-m-d H:i:s')  
					 );
	 $result =	$this->m_impact->insert('tbl_setting', $data_arr, '');
	 if($result){
		 $this->session->set_userdata('message', $this->connect->message("Setting has been created!", "success"));
	 }else{
		 $this->session->set_userdata('message', $this->connect->message("Setting has been created with error!", "warning"));
	 }
	 
	 redirect(site_url().'/c_setting/page_new', 'location', 302);
 }
 /*
 page edit
 */
 function page_edit($id = false){
	 $id = $this->m_impact->encrypt_decrypt('decrypt', $id);
	 $where_type = array();
	 $data['setting_types']	=	$this->m_impact->get_by_fields('tbl_setting_type',$where_type,'id');
	 
	 $where = array('id' => $id);
	 $data['setting']	=	array_shift($this->m_impact->get_by_fields('tbl_setting',$where,'id'));
	 
	 $data['body']='setting/v_page_edit';
	 $this->load->view('desktop/desktop',$data);
 }
 
 /*
 page update data
 */
 function update(){
	$id = $this->m_impact->encrypt_decrypt('decrypt', $this->input->post('id'));
	$where	= array('id' => $id);
	$data_arr = array(
						'setting_type_id'			=> $this->m_impact->encrypt_decrypt('decrypt', $this->input->post('setting_type_id')),
						'name'			=> $this->input->post('name'),
						'lat'			=> $this->input->post('lat'),
						'lng'			=> $this->input->post('lng'),
						'description'	=> $this->input->post('description'), 
						'image' 		=> $this->input->post('image'),  
						'user_update' 	=> $this->session->userdata('name'), 
	 				    'update_date' 	=> date('Y-m-d H:i:s')  
					 );
	$result	=	$this->m_impact->update_field('tbl_setting', $data_arr, $where);
	
	if($result){
		$this->session->set_userdata('message', $this->connect->message("tbl_setting has been updated!", "success"));
	}else{
		$this->session->set_userdata('message', $this->connect->message("tbl_setting has been updated with error!", "warning"));
	}

	redirect(site_url().'/c_setting', 'location', 302);
 }
 
 /*
 page update data
 */
 function delete($id, $status){
	$id = $this->m_impact->encrypt_decrypt('decrypt', $id);
	
	$status = ($status == 1) ? 0 : 1;
	$where = array('id' => $id);
	
	$data_arr = array(
						'status'			=> $status,
						'user_update' 	=> $this->session->userdata('name'), 
	 				    'update_date' 	=> date('Y-m-d H:i:s')  
					 );
	$result	=	$this->m_impact->update_field('tbl_category', $data_arr, $where);
	
	if($result){
		$this->session->set_userdata('message', $this->connect->message("Successful!", "success"));
	}else{
		$this->session->set_userdata('message', $this->connect->message("Updated error!", "warning"));
	}
	
	redirect(site_url().'/c_category', 'location', 302);
 }
 
}
// end c_promotion

?>
